import java.util.ArrayList;
import java.util.List;

public class PokerGame {
	
	private List<PokerPlayer> players = new ArrayList<PokerPlayer>();
	private PokerDeck deck;
	private int personPosition;
	private int dealerPosition;
	private String gameState;
	private int nextPlayer;
	private int smallBlind;
	private int bigBlind;
	private int toCall;
	
	PokerGame(int playerCount, int startingScore, int person) {
		//init game variables
		this.smallBlind = 10;
		this.bigBlind = 20;
		this.toCall = this.bigBlind;
		this.personPosition = person;
		this.dealerPosition = (int)(10.0 * Math.random());
		if (this.dealerPosition == 7) this.nextPlayer = 0;
		else this.nextPlayer = this.dealerPosition + 3;
		//init players and deal cards
		this.deck = new PokerDeck();
		PokerPlayer player;
		players.clear();
		for (int i = 0; i < playerCount; i++) {
			player = new PokerPlayer("player"+1, startingScore);
			player.setCard1(this.deck.deal());
			player.setCard2(this.deck.deal());
			player.setPosition(i);
			player.setBet(0);
			player.setToCall(this.bigBlind);
			player.setStatus("waiting...");
			players.add(player);			
		}
		//adjust scores to blinds		
		this.gameState = "Preflop";
		this.setBlinds();
	}
	
	private void setBlinds() {
		int small,big;
		if (this.dealerPosition == 9) small = 0;						
		else small = this.dealerPosition + 1;
		if (this.dealerPosition == 8) big = 0;
		else big = this.dealerPosition + 2;
		this.players.get(small).setScore(this.players.get(small).getScore() - this.smallBlind);
		this.players.get(small).setStatus("small blind : "+Integer.toString(this.smallBlind));
		this.players.get(big).setScore(this.players.get(big).getScore() - this.bigBlind);
		this.players.get(big).setStatus("big blind : "+Integer.toString(this.bigBlind));
		this.players.get(small).setBet(this.smallBlind);
		this.players.get(big).setBet(this.bigBlind);		
	}
	
	public void advanceToPerson() {
		while (this.nextPlayer != this.personPosition) {
			//TODO: fix the indexOUtOfbounds here
			//System.out.println("Dealer : "+this.dealerPosition+" Player : "+this.personPosition+" next : "+this.nextPlayer);			
			this.players.get(this.nextPlayer).doAction(this.dealerPosition,this.toCall);
			if (this.nextPlayer == 9 ) this.nextPlayer = 0;
			else this.nextPlayer = this.nextPlayer + 1;						
		}
	}
	
	public String getGameState() {
		return gameState;
	}

	public int getDealerPosition() {
		return dealerPosition;
	}
	public PokerPlayer getPlayer(int position) {
		return this.players.get(position);
	}	
	public int getPersonPosition() {
		return this.personPosition;
	}	
}
